package ar.uba.fi.taller2.jh.BBasicTree;

import java.util.*;

import ar.uba.fi.taller2.jh.Tree.*;

/*****************************************************************************
 * <p>Title: CMSC420 </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2004</p>
 *
 * <p>Company: </p>
 *
 * @author Sebastian rivas
 * @version 1.0
 * 
 ****************************************************************************/

public abstract class BBasicLeafNode extends LeafNode {
  

  /****************************************************************************
   * CONSTRUCTOR
   *
   * @param comp Comparator
   * @param n_recs int
   ***************************************************************************/

  public BBasicLeafNode(Comparator comp,int n_recs, boolean _isRoot, Tree _tree) {
	  super(comp, n_recs, _isRoot, _tree);
  }

  /****************************************************************************
   * CONSTRUCTOR
   *
   * @param o BSLeafNode
   ***************************************************************************/

  public BBasicLeafNode(BBasicLeafNode o) {
	  super(o);
  }

  /****************************************************************************
   *
   * @param o Object
   *  append a child or record to the end
   ***************************************************************************/
  public void append(Object o, Object key){

	 Tree.Entry entry = new Tree.Entry();
	 entry.setKey(key);
	 entry.setValue("");
	  
     copyRecord(rsize, entry);
     rsize++;
   }


   /****************************************************************************
    *
    * @param o Object
    *  puts something at the BBasicginning of the children or records
    ***************************************************************************/
   public void putFirst(Object o, Object key){
	 Tree.Entry entry = new Tree.Entry();
	 entry.setKey(key);
	 entry.setValue("");
     copyRecord(0,entry);
   }

  // Concatena el nodo next
  public void concat(Node next, Object key) {
	  
	  Tree.Entry entry = new Tree.Entry();
	  entry.setKey(key);
	  entry.setValue("");
	  
	  this.copyRecord(rsize, entry);
	  BBasicLeafNode nextAux = (BBasicLeafNode)next;
	  for(int i=0; i<nextAux.size(); i++) {
		  this.copyRecord(rsize+i+1, nextAux.records[i]);
	  }
	  this.rsize = this.size()+nextAux.size()+1;
	  nextAux.tree.getDisk().releaseBlock(this.getId());
  }

 public Object getPromoted(int pos) {
	 if(pos>=rsize)
		 return null;
	 
	 return this.records[pos].getKey();
 }
}